package top.lfyao.designpattern.details.factory.abstraction;

import top.lfyao.designpattern.details.factory.simple.Operation;
import top.lfyao.designpattern.details.factory.simple.OperationAdd;
import top.lfyao.designpattern.details.factory.simple.OperationMultiplication;
import top.lfyao.designpattern.details.factory.simple.OperationSubtraction;

import java.util.HashMap;
import java.util.Map;

public class OperationFactory {
    private static Map<String, Class<?>> allOperationMaps = new HashMap<>();

    private static void fillMap() {
        allOperationMaps.put("+", OperationAdd.class);
        allOperationMaps.put("-", OperationSubtraction.class);
        allOperationMaps.put("*", OperationMultiplication.class);
        allOperationMaps.put("/", OperationSubtraction.class);
    }

    public static Operation createOperation(String operator) throws IllegalAccessException, InstantiationException {
        Operation operation;
        fillMap();
        Class<?> operationClass = allOperationMaps.get(operator);
        if (operationClass==null) throw new RuntimeException("unsupported operation");
        Operation o = (Operation)operationClass.newInstance();
        return o;
    }

}
